package com.crowntit.domain.activity.service.trail.node;

import com.alibaba.fastjson.JSON;
import com.crowntit.domain.activity.model.entity.MarketProductEntity;
import com.crowntit.domain.activity.model.entity.TrialBalanceEntity;
import com.crowntit.domain.activity.service.trail.AbstractGroupBuyMarketSupport;
import com.crowntit.domain.activity.service.trail.factory.DefaultActivityStrategyFactory;
import com.crowntit.types.design.framework.tree.StrategyHandler;
import com.crowntit.types.enums.ResponseCode;
import com.crowntit.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class SwitchNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private MarketNode marketNode;

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼团商品查询试算服务-SwitchNode userId:{} requestParameter:{}", requestParameter.getUserId(), JSON.toJSONString(requestParameter));

        String userId = requestParameter.getUserId();

        if (activityRepository.isDegrade()){
            log.info("拼团活动降级拦截 {} --- 放行", userId);
            //throw new AppException(ResponseCode.E0003.getCode(), ResponseCode.E0003.getInfo());
        }

        if (activityRepository.isCutRange(userId)){
            log.info("拼团活动切量拦截 {} --- 放行", userId);
            //throw new AppException(ResponseCode.E0004.getCode(), ResponseCode.E0004.getInfo());
        }

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return marketNode;
    }
}
